package com.example.texting;

import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class TextEncryptor extends Activity {
		
	Button btnView;
    Button btnSendSMS;
    Button btnCrypt;
    EditText txtPhoneNo;
    EditText txtMessage;
    Button btnDecrypt;
    private static final String TAG = "TextEncryptorHIT";
    
    // BroadcastRecieve to know when you get a new text
    IntentFilter seeSMS = new IntentFilter("com.example.texting.SMSreceiver");
    SMSreceiver receiver = new SMSreceiver();
 
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        registerReceiver(receiver, seeSMS);
        
        // Variables to set new messages
        String message;
        String numPh;

        
        btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
        btnView = (Button) findViewById(R.id.btnSwitchView);
        btnCrypt = (Button) findViewById(R.id.btnEncrypt);
        txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
        txtMessage = (EditText) findViewById(R.id.txtMessage);
        btnDecrypt = (Button) findViewById(R.id.btnDecrypt);
		Log.i(TAG, "Program has started up and buttons impemented.");


        // Notices when the View Switch button is clicked.  Will switch to see the recieved messages.
        btnView.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
        		Log.i(TAG, "Button Has been pressed!");
            	Intent messageIntent = new Intent();
            	messageIntent.setClassName("com.example.texting", "com.example.texting.viewMessages");
            	startActivity(messageIntent);

            }
        });
        
        // Notices when the Send Message button is clicked
        btnSendSMS.setOnClickListener(new View.OnClickListener() 
        {


            public void onClick(View v) 
            {
        		Log.i(TAG, "Button Has been pressed!");
                String phoneNo = txtPhoneNo.getText().toString();
                String message = txtMessage.getText().toString();                 
                if (phoneNo.length()>0 && message.length()>0)
                {
                    SmsManager txtTest = SmsManager.getDefault();
                	txtTest.sendTextMessage(phoneNo, null, message, null, null);
                    Toast.makeText(getBaseContext(), 
                            "SENT!", 
                            Toast.LENGTH_SHORT).show();
                }
                else
                    Toast.makeText(getBaseContext(), 
                        "Message and Phone Number are required.", 
                        Toast.LENGTH_SHORT).show();
            }
        });
        
        // Notices when the Encrypt button is pressed
        btnCrypt.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
        		Log.i(TAG, "Button Has been pressed to Encrypt!");

            	String message = txtMessage.getText().toString();
            	char[] StringArray = message.toCharArray();
            	int num;
    			int Encrypt[][] = {{1,3,3},{1,4,3},{1,3,4}};    			    			
    			
    			if(StringArray.length%3==0)
    				num = StringArray.length/3;
    			else
    				num = StringArray.length/3+1;
    			
    			
    			int[][] CharMatrix = new int[3][num];
    			int[][] EncryptedMessage = new int[3][num];
    			int index = 0;
    			
    			for (int i=0; i<3; i++){
    				for (int j=0; j<num; j++){
    					if(index>=StringArray.length) 	//If you reach the end of the message, fill in remaining spots with spaces
    						CharMatrix[i][j] = 0;
    					else if ((int) StringArray[index]>=32 && (int) StringArray[index]<=126) //If in list of normal characters
    						CharMatrix[i][j] = (int) StringArray[index]-32;
    					else //Changes special characters to spaces
    						CharMatrix[i][j] = 0;
    					index++;
    				}
    			}
    					
    			//Encrypt Message
    			for (int i=0;i<CharMatrix[0].length;i++){
    				for (int j=0; j<CharMatrix.length; j++){
    					EncryptedMessage[j][i] = (Encrypt[j][0]*CharMatrix[0][i]+Encrypt[j][1]*CharMatrix[1][i]+Encrypt[j][2]*CharMatrix[2][i])%94;
    				}
    			}
    			
    			String NewMessage = "";
    			index = 0;
    			for (int i=0; i<3; i++){
    				for (int j=0; j<num; j++){
    					//StringArray[index]=(char) (EncryptedMessage[i][j]+32);
    					NewMessage += (char) (EncryptedMessage[i][j]+32);
    				}
    			}
    			
    			
    			txtMessage.setText(NewMessage);
            }
        });
        // Notices when the Decrypt button is pressed
        btnDecrypt.setOnClickListener(new View.OnClickListener() 
        {
            public void onClick(View v) 
            {
            	String message = txtMessage.getText().toString();
            	char[] StringArray = message.toCharArray();
            	int num;
    			int Decrypt[][] = {{7,-3,-3},{-1,1,0},{-1,0,1}};   			    			
    			
    			if(StringArray.length%3==0)
    				num = StringArray.length/3;
    			else
    				num = StringArray.length/3+1;
    			
    			
    			int[][] CharMatrix = new int[3][num];
    			int[][] DecryptedMessage = new int[3][num];
    			int index = 0;
    			
    			for (int i=0; i<3; i++){
    				for (int j=0; j<num; j++){
    					if(index>=StringArray.length) 	//If you reach the end of the message, fill in remaining spots with spaces
    						CharMatrix[i][j] = 0;
    					else if ((int) StringArray[index]>=32 && (int) StringArray[index]<=126) //If in list of normal characters
    						CharMatrix[i][j] = (int) StringArray[index]-32;
    					else //Changes special characters to spaces
    						CharMatrix[i][j] = 0;
    					index++;
    				}
    			}
    					
    			//Decrypt Message
    			for (int i=0;i<CharMatrix[0].length;i++){
    				for (int j=0; j<CharMatrix.length; j++){
    					DecryptedMessage[j][i] = (Decrypt[j][0]*CharMatrix[0][i]+Decrypt[j][1]*CharMatrix[1][i]+Decrypt[j][2]*CharMatrix[2][i])%94;
    					while(DecryptedMessage[j][i]<0){
    						DecryptedMessage[j][i]=DecryptedMessage[j][i] + 94;
    					}
    				}
    			}
    			
    			String NewMessage = "";
    			for (int i=0; i<3; i++){
    				for (int j=0; j<num; j++){
    					NewMessage += (char) (DecryptedMessage[i][j]+32);
    				}
    			}
    			
    			txtMessage.setText(NewMessage);
            	
            }
        });
        
		Intent getMsgIntent = getIntent();
		message = getMsgIntent.getStringExtra("msg");
		numPh = getMsgIntent.getStringExtra("phnum");
		
		txtMessage.setText(message);
		txtPhoneNo.setText(numPh);

    }
}

